<?php 
	namespace Home\Controller;
	use Think\Controller;

	// class DownloadController extends AuthController
	class DownloadController extends Controller
	{
		public function index(){return;}
		public function get_code_rate(){
			if(IS_POST){
				$userid = session("user_id");
				$type = M('UserProvince')->alias('a')->where(['user_id'=>$userid])->join('t_province as b on a.province_id = b.id')->getField('type');
				$type_list = explode(',', $type);
				$result = array_map(function($e){
					$code_type = [
						'1' => '2.5M_VBR',
						'2' => '2.5M_CBR',
						'3' => '4M_VBR',
						'4' => '4M_CBR',
						'5' => '8M_CBR',
						'6' => '8M_VBR'
					];
					return ['value'=>trim($e),'name'=>$code_type[trim($e)]];
				}, $type_list);
				echo json_encode($result);
				exit;
			}
		}
		public function download_movie()
		{
			// 接收节目id  查出对应节目下载地址
			if(IS_POST){
		 		$refererUrl = parse_url($_SERVER['HTTP_REFERER']);
				if(!isset($_SERVER['HTTP_REFERER']) || 'gyp.meidongya.cn/html/juji.php' != $refererUrl['host'].':'.$refererUrl['port'].$refererUrl['path']) {
					exit('An error is occured!');
				}

				$userid = session("user_id");
				$code_type = D("Province")->join('user_province ON user_province.province_id = t_province.id')->where(["user_id"=>$userid])->getField("type");
				// dump($code_type);exit();   //码率对应值
				$program_id = I("post.pid","");	
				$post_code_rate = I("post.code_rate","");
				$code_type_array = explode(',', $code_type);
				if($program_id != "" && $program_id != null && $program_id && $code_type && in_array($post_code_rate, $code_type_array)){
					$res = D("TDownload")->get_movie($program_id,$post_code_rate);
					if($res){
						$this->assign(['data'=>$res]);
						$this->display();
					}else{
						echo "没有视频信息";
					}
				}else{
					echo "节目未找到";
				}
			}		
		}

		/**
		 * 节目下载统计
		 * @return [type] [description]
		 */
		public function statistics()
		{
			if(IS_POST){
				 $userid = session("user_id");
				 $rows = I("rows","25");
				 $page = I("page","1");
				 $order = I("order",'','strval');
				 $province_name = I("province_name","");
				 $order = strtolower($order) == 'asc'?'ASC':'DESC';
				 $res_role = $this->get_roleflag($userid);
				 $province_name1 = $this->get_pro($userid);
				 if($res_role == 1){
				 	  if($province_name){
						 $province_name_all[] = I("province_name","");	
				 	  }else{
				 	  	 $province_name_all = [];
				 	  }
				 }else{
				 	  foreach ($province_name1 as $key => $value) {
					 	  $province_name_all[$key] = $value['province'];
					 }
				 }
				 if(!empty($province_name_all)){
				 	$where['province'] = ['in',$province_name_all];
				 }
				 $program_name = I("program_name","");
				 if(!empty($program_name)){
				 	$where['program_name'] = ['LIKE',"%".$program_name."%"];
				 }
				 $code_rate = I("code_rate","");
				 if(!empty($code_rate)){
				 	$where['code_rate'] = ['IN',"$code_rate"];
				 }				 
				 $download_status = I("download_status",'');
				 if($download_status != ""){
				 	$where['a.download_status'] = $download_status;
				 }
				 $page = I("page","1");
				 $first = ($page-1)*$rows;
				 $Model = new \Think\Model();
				 $total = M('TDownload')->alias('a')->join('JOIN movie as b ON a.movie_id = b.id JOIN t_program as c ON b.program_id = c.id JOIN t_province as d on a.province_id = d.id')->where($where)->count();
				 $res = M('TDownload')->field('a.id,a.create_time,a.update_time,a.download_status,b.code_rate,c.program_name,d.province')->alias('a')->join('JOIN movie as b ON a.movie_id = b.id JOIN t_program as c ON b.program_id = c.id JOIN t_province as d on a.province_id = d.id')->where($where)->limit($first,$rows)->order('a.update_time '.$order)->select();
				 // dump($res);exit();
				 // $res = $Model->query("SELECT a.id,a.update_time,a.download_status,b.code_rate,c.program_name,d.province from t_download as a 
			  //               JOIN movie as b ON a.movie_id = b.id 
			  //               JOIN t_program as c ON b.program_id = c.id
			  //               JOIN t_province as d on a.province_id = d.id
			  //               where c.program_name like '%{$program_name}%'
			  //               and d.province like '%{$province_name}%'
			  //               ORDER BY a.update_time 
			  //               limit {$first},{$rows};");
				 // $total = $Model->query("SELECT count(1) from t_download as a 
			  //               JOIN movie as b ON a.movie_id = b.id 
			  //               JOIN t_program as c ON b.program_id = c.id
			  //               JOIN t_province as d on a.province_id = d.id
			  //               where c.program_name like '%{$program_name}%'
			  //               and d.province like '%{$province_name}%'"); 
			//  echo M('TDownload')->fetchSql(true)->field('a.*,b.program_name,c.province')->alias('a')->join('LEFT JOIN t_program as b ON a.program_id = b.id LEFT JOIN t_province as c on a.province_id = c.id')->where([
			// 	'program_name'=>['LIKE','%'.$program_name.'%'],
			// 	'province'=>['LIKE','%'.$province_name.'%']
			// ])->order('a.update_time')->limit($first.','.$rows)->select();
				 $res?$res:$res=[];
				 // $total = count($res);
				 $output['total'] = $total;
				 $output['rows'] = $res;
				 // dump($output);exit();
				 echo json_encode($output);
			}else{
				 // $Model = new \Think\Model();
				 // $res = $Model->query("SELECT a.*,b.program_name,c.province from t_download as a 
					// 			LEFT JOIN t_program as b ON a.program_id = b.id 
					// 			LEFT JOIN t_province as c on a.province_id = c.id;");
				 // $res?$res:$res=null;
				 // echo json_encode($res);
			}
		}

		public function get_roleflag($userid)
		{
			$res = D("SysUser")->where(["id"=>$userid])->field("role_id")->find();
			$res?$res:$res="";
			return $res['role_id'];
		}

		public function get_pro($userid)
		{
			// dump($userid);exit;
			$Model = new \Think\Model();
			$sql = "SELECT a.province FROM t_province as a LEFT JOIN user_province as b on a.id = b.province_id WHERE b.user_id = {$userid};";
			$res = $Model->query($sql);
			// dump($res);exit();
			$res?$res:$res="";
			return $res;
		}

		/**
		 * 接口访问日志记录
		 */
		public function apistatic()
		{
			$page = I("post.page","1");
			$rows = I("post.rows","25");
			$first = ($page-1)*$rows;
			$province = I("post.province_name","");
			$total = D("Dispatch")->order('time desc')->count();
			if($province == ""){
				$all = D("Dispatch")->alias('a')->join("t_province as b on a.province_id = b.id")->field("a.*,b.province,b.id as province_id")->order('time desc')->limit($first,$rows)->select();
			}else{
				$province_id = D('Province')->where(["province"=>$province])->getField("id");
				$all = D("Dispatch")->alias('a')->join("t_province as b on a.province_id = b.id")->field("a.*,b.province,b.id as province_id")->where(["province_id"=>$province_id])->order('time desc')->limit($first,$rows)->select();
			}			
			$output = [
				"total" => $total,	
				"rows" => $all	
			];
			echo json_encode($output);
		}

	}